from utlis.mysql import db
from utlis.my_jwt import jj

import time,json
from myredis import r
# 权限验证装饰器
def pomition():
    def is_login(func):
        def wrapper(self, *args, **kargs):
            # 业务
            # 获取token
            headers = self.request.headers

            print('!!!!!',headers)
            try:
                token = headers['token']
                print('======',token)
                flag = jj.check(token)
                print("flag>>>>>>>>>",flag)
                if flag == True:
                    # 解析验证时间
                    payload = jj.jwt_decode(token)
                    print("payload>>>>>>>>",payload)
                    ptime = payload['data']['exp']
                    print("ptime>>>>>>>>>>",ptime)
                    now = time.time()
                    if int(now)-int(ptime) > 36:
                        self.finish({"code": 402,"code":"token过期"})
                else:
                    self.finish({"code": 403})
            except:
                self.finish({"code": 401})
            # 验证token
            # headers['token']
            # 解析获取时间和当前时间对比
            func(self, *args, **kargs)
        return wrapper
    return is_login
def menulist(self,uid):
    sql = f"""select re.*,pre.name as pname from role_resource as rr left join resource as re on rr.resourceid=re.id left join resource as pre on pre.id = re.pid where rr.userid={uid}"""
    res = db.findAll(sql)
    list = []
    menulist = []
    for i in res:
        if i['pid'] not in menulist:
            list.append({'id': i['pid'], 'name': i['pname'], 'son': []})
            menulist.append(i['pid'])
    for j in res:
        for index, p in enumerate(list):
            if j['pid'] == p['id']:
                list[index]['son'].append({'id': j['id'], 'name': j['name'], 'url': j['url']})
    self.write({'code': 200, 'list': list})


